package com.haisen.service.impl;


import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class HiveJdbcTemplate {


    @Autowired
    @Qualifier("hiveDruidTemplate")
    private JdbcTemplate jdbcTemplate;




    @Test
    public void testLoad(){
        String sql = "select count(*) from psn2";
        Long count = (Long)jdbcTemplate.queryForObject(sql,Long.class);
        log.info("条数={}",count);
    }


    /**
     * ddl
     */
    @Test
    public void testexecute(){
        //不能要冒号;
        // String sql ="show databases";
        String sql ="create table psn3(\n" +
                "id int,\n" +
                "name string,\n" +
                "age int,\n" +
                "sex string,\n" +
                "likes array<string>,\n" +
                "address map<string,string>\n" +
                ")\n" +
                "row format delimited\n" +
                "fields terminated by ','\n" +
                "collection items terminated by '-'\n" +
                "map keys terminated by ':'\n" +
                "lines terminated by '\\n'";
         jdbcTemplate.execute(sql);
    }


    /**
     * dml curd
     */
    @Test
    public void testCURD(){
        String sql = "insert into psn3(id,name,age,likes,address)\n" +
                "           select 2,'xiaoming2',30,array('lol','book','movie'),str_to_map('beijing:shangxuetang,shanghai:pudo',',',':')";
        jdbcTemplate.update(sql);
    }

}
